package com.vates.reservas.web;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vates.reservas.dominio.Hotel;
import com.vates.reservas.service.HotelService;

/**
 * Handles and retrieves the main requests
 */
@Controller
@RequestMapping(value = "/hotel/editar")
public class HotelEditarController {

	protected static Logger logger = Logger.getLogger("controller");

	@Autowired
	private HotelService hotelService;

	/**
	 * Handles and retrieves the AJAX Add page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String gethotelEditar(Model model) {
		model.addAttribute(new Hotel());
		return "hotel/editar";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Hotel hotel, BindingResult result) {
		if (result.hasErrors()) {
			return "hotel/editar";
		}
		return "redirect:/hotel/editar/" + hotel.getId();
	}

	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public String gethotelModificar(@PathVariable Long id, Model model) {
		model.addAttribute(hotelService.busquedaPorId(id));
		return "hotel/modificar";
	}
	@RequestMapping(method=RequestMethod.POST, value="modificar")
	public String modificar(@Valid Hotel hotel, BindingResult result) {
		if (result.hasErrors()) {
			return "hotel/editar";
		}
		hotelService.editar(hotel.getId(), hotel.getNombre(), hotel.getCategoria(), hotel.getDomicilio(), hotel.getLocalidad(), hotel.getProvincia(), hotel.getCodigoPostal(), hotel.getTelefono());
		return "redirect:/hotel/listado/";
	}

}